<?php 

	require "config.php";
	
	$lekerdezes = "SELECT * FROM music WHERE id=$_GET[musicid]";
	$talalt_z = $conn->query($lekerdezes);
	$zene = $talalt_z->fetch_assoc();
	
	// Hozzáadás a kedvencekhez
	if(isset($_POST['add-btn'])){
		$conn->query("INSERT INTO favs VALUES(id, $_COOKIE[id], $_GET[musicid])");
		header("Location: music.php?musicid=$_GET[musicid]");
	}
	
	// Törlés a kedvencek közül
	if(isset($_POST['del-btn'])){
		$conn->query("DELETE FROM favs WHERE userid=$_COOKIE[id] AND musicid=$_GET[musicid]");
		header("Location: music.php?musicid=$_GET[musicid]");
	}
	
	// Listához adás
	if(isset($_POST['add-btn'])){
		if($_POST['lista'] != 0){
			$conn->query("INSERT INTO music_in_playlist VALUES(id, $_POST[lista], $_GET[musicid])");
			header("Location: music.php?musicid=$_GET[musicid]");
		}
	}
	
?>
<p><?= $zene['name']; ?></p>
<audio controls>
	<source src="files/<?= $zene['file_name']; ?>" type="audio/mp3">
</audio>
<br>
<br>
<form method="post">
	<?php 
	
		$lekerdezes = "SELECT * FROM favs WHERE userid=$_COOKIE[id] AND musicid=$_GET[musicid]";
		$talalt_k = $conn->query($lekerdezes);
		
		if(mysqli_num_rows($talalt_k) == 0){
			echo '<input type="submit" name="add-btn" value="Hozzáadás a kedvencekhez">';
		}
		else{
			echo '<input type="submit" name="del-btn" value="Törlés a kedvencek közül">';
		}
		
	?>
</form>

<form method="post">
	<select name="lista">
		<option value="0">Lista kiválasztása</option>
		<?php 
			$lekerdezes = "SELECT * FROM playlists WHERE userid=$_COOKIE[id]";
			$talalt_l = $conn->query($lekerdezes);
			while($lista=$talalt_l->fetch_assoc()){
		?>
			<option value="<?= $lista['id']; ?>"><?= $lista['name']; ?></option>
		<?php } ?>
	</select>
	<input type="submit" name="add-btn" value="Hozzáadás">
</form>